<template>
  <div class="app-container">
    <el-row>
       <div class="fromDiv">
      <h1>活动表单设置</h1>
    </div>
    </el-row>
   
     <el-row style="text-align:center">
    <el-form ref="form" :model="form" label-width="100px" style="display:inline-block" sizs="mini">
    <el-row :gutter="20">
    <el-col :span="12">
    <el-form-item label="活动标题" >
    <el-input   :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="12">
    <el-form-item label="轮播海报" >
    <el-input   :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    </el-row>
     <el-row :gutter="20">
    <el-col :span="12">
    <el-form-item label="活动详情" >
    <el-input   :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="12">
    <el-form-item label="报名时间" >
    <el-input  :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    </el-row>
     <el-row :gutter="20">
    <el-col :span="12">
     <el-form-item label="报名人数" >
    <el-input  :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="12">
    <el-form-item label="报名费用" >
    <el-input  :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    </el-row>
     <el-row :gutter="20">
    <el-col :span="12">
    <el-form-item label="活动时间" >
    <el-input   :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="12">
    <el-form-item label="活动地点" >
    <el-input   :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    </el-row>
     <el-row :gutter="20">
    <el-col :span="12">
    <el-form-item label="活动联系人" >
    <el-input   :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    <el-col :span="12">
    <el-form-item label="联系方式" >
    <el-input  :disabled="true" ></el-input>
  </el-form-item>
    </el-col>
    </el-row>
    <el-row :gutter="20" v-for="(item,index) in customList" :key="index">
    <el-col :span="12" >
    <el-form-item :label="getName(item.name)" >
    <el-col :span="20">
     <el-input  :disabled="true" >
     </el-input>
     </el-col>
   <el-col :span="4">
   <el-button
   size="mini"
    icon="el-icon-delete"
    circle
    @click="delCustom(item)"
     />
    </el-col>
   </el-form-item>
    </el-col>
    </el-row>
  <el-form-item>
    <el-button type="primary" @click="handleAdd">新增字段</el-button>
  </el-form-item>
 
</el-form>
 </el-row>

    <!-- 添加或修改活动报名展示自定字段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :model="dynamicValidateForm" ref="dynamicValidateForm"  label-width="100px" class="demo-dynamic" :rules="simpleRules">
        <el-form-item label="字段类型" porp="type" 
        :rules="{
            required: true, message: '字段类型不能为空', trigger: 'blur'
           }"
        >
         <el-select v-model="dynamicValidateForm.type" placeholder="请选择" @change="updataValue" 
        
         >
          <el-option
            v-for="item in dict.type.sys_consutom_type"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="是否必填" prop="required" 
        
        >
          <el-select v-model="dynamicValidateForm.required" placeholder="请选择">
          <el-option
            v-for="item in requiredOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        </el-form-item> 
        <el-form-item label="名称" prop="name"
         :rules="{
            required: true, message: '名称不能为空', trigger: 'blur'
           }">
          <el-input v-model="dynamicValidateForm.name" placeholder="请输入内容" style="width:220px"></el-input>
        </el-form-item> 
        <el-row v-if="changeValue == 'radio' || changeValue == 'checkbox' || changeValue =='picker'">
          <el-form-item  
          v-for="(domain, index) in dynamicValidateForm.domains"
          :label="'选项' + index"
          :key="domain.key"
          :prop="'domains.' + index + '.value'"
          :rules="{
            required: true, message: '域名不能为空', trigger: 'blur'
           }"
        >
        <el-col :lg="9"><el-input v-model="domain.value" style="width:220px"></el-input></el-col>
        <el-col :lg="15"><el-button @click.prevent="removeDomain(domain)">删除</el-button></el-col>
        
  </el-form-item>
  <el-form-item>
    <el-button @click="addDomain">新增选项</el-button>
    <el-button @click="reset('dynamicValidateForm')">重置</el-button>
  </el-form-item>
        </el-row>
</el-form>
<div slot="footer" class="dialog-footer">
  <el-button type="primary" @click="submitcForm()">保存</el-button>
  <el-button @click="cancel">取 消</el-button>
</div>
    </el-dialog>

  </div>
</template>

<script>
import { listActivityCustom, getActivityCustom, delActivityCustom, addActivityCustom, updateActivityCustom } from "@/api/activity/activityCustom";
import { addCustom } from "@/api/activity/custom";


import { type } from "os";

export default {
  name: "Custom",
  dicts: ['sys_consutom_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动报名展示自定字段表格数据
      customList: [],
      changeValue:'',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      requiredOptions:[
        { value:0,
          label:"非必填"
        },
        { value:1,
          label:"必填"
        },
      ],
      //多选框，单选框弹出层
      comstunOpen:false,
      dynamicValidateForm: {
        
        },
     
      changeType:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityId: null,
        name: null,
        type: null,
        required: null,
        place: null,
        data: null,
        status: 0,
        sourceType: 1
      },
      // 表单参数
      form: {},
      
    };
  },
  created() {
    this.getList();
  },
  methods: {
    updataValue(val){
      console.info(val);
      this.changeValue=val;
    },
    //自定义活动内容
    submitcForm() {
        this.$refs["dynamicValidateForm"].validate((valid) => {
          if (valid) {
            console.info(this.dynamicValidateForm);
            addCustom(this.dynamicValidateForm).then(res=>{
             this.$modal.msgSuccess("新增成功");
             this.open=false;
             this.getList();
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      removeDomain(item) {
        var index = this.dynamicValidateForm.domains.indexOf(item)
        if (index !== -1) {
          this.dynamicValidateForm.domains.splice(index, 1)
        }
      },
    addDomain() {
        this.dynamicValidateForm.domains.push({
          value: '',
          key: Date.now()
        })},
    getName(data){
      return data;
    },
    delCustom(data){
      console.info(data.id);
    },
    /** 查询活动报名展示自定字段列表 */
    getList() {
      this.loading = true;
      listActivityCustom(this.queryParams).then(res => {
        console.info(res)
        this.customList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.dynamicValidateForm = {
        domains: [{
            value: ''
          }],
          required: '',
          type:'',
          name:''
      }; 
      this.resetForm(this.dynamicValidateForm);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动报名展示自定字段";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivityCustom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动报名展示自定字段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivityCustom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivityCustom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动报名展示自定字段编号为"' + ids + '"的数据项？').then(function() {
        return delActivityCustom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
<style >
.fromDiv{
  height: 50px;
  box-shadow: 0 2px 40px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  text-align: center;
  line-height: 50px;
  margin-bottom: 50px ;
  background-color: aqua;
}
h1{
  height:30px; line-height:50px;
}

</style>
